<template>
  <div class="comment-container">
    <el-card class="comment-card">
      <template #header>
        <div class="card-header">
          <span>评论管理</span>
          <div class="header-operations">
            <el-input
              v-model="searchQuery"
              placeholder="搜索用户名/书籍名称"
              style="width: 200px; margin-right: 10px;"
            />
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </div>
        </div>
      </template>
      
      <el-table :data="commentList" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="bookName" label="书籍名称" />
        <el-table-column prop="userName" label="评论用户" width="120" />
        <el-table-column prop="content" label="评论内容" show-overflow-tooltip />
        <el-table-column prop="rating" label="评分" width="100">
          <template #default="scope">
            <el-rate
              v-model="scope.row.rating"
              disabled
              show-score
              text-color="#ff9900"
            />
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === '正常' ? 'success' : 'danger'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="评论时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button 
              :type="scope.row.status === '正常' ? 'danger' : 'success'" 
              size="small" 
              @click="handleToggleStatus(scope.row)"
            >{{ scope.row.status === '正常' ? '隐藏' : '显示' }}</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
            <el-button type="primary" size="small" @click="handleDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 评论详情对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="评论详情"
      width="600px"
    >
      <el-descriptions :column="2" border>
        <el-descriptions-item label="ID">{{ currentComment.id }}</el-descriptions-item>
        <el-descriptions-item label="书籍名称">{{ currentComment.bookName }}</el-descriptions-item>
        <el-descriptions-item label="评论用户">{{ currentComment.userName }}</el-descriptions-item>
        <el-descriptions-item label="评分">
          <el-rate
            v-model="currentComment.rating"
            disabled
            show-score
            text-color="#ff9900"
          />
        </el-descriptions-item>
        <el-descriptions-item label="评论内容" :span="2">{{ currentComment.content }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <el-tag :type="currentComment.status === '正常' ? 'success' : 'danger'">
            {{ currentComment.status }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="评论时间">{{ currentComment.createTime }}</el-descriptions-item>
        <el-descriptions-item label="点赞数">{{ currentComment.likes }}</el-descriptions-item>
        <el-descriptions-item label="回复数">{{ currentComment.replies }}</el-descriptions-item>
      </el-descriptions>

      <!-- 回复列表 -->
      <div class="reply-list" v-if="currentComment.replyList && currentComment.replyList.length > 0">
        <div class="reply-title">回复列表</div>
        <div v-for="reply in currentComment.replyList" :key="reply.id" class="reply-item">
          <div class="reply-user">{{ reply.userName }}</div>
          <div class="reply-content">{{ reply.content }}</div>
          <div class="reply-time">{{ reply.createTime }}</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索相关
const searchQuery = ref('')

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 评论列表数据
const commentList = ref([
  {
    id: 1,
    bookName: '示例书籍1',
    userName: '用户A',
    content: '这是一条评论内容，写得很好，推荐阅读...',
    rating: 4.5,
    status: '正常',
    createTime: '2024-04-05 10:00:00',
    likes: 10,
    replies: 2,
    replyList: [
      {
        id: 1,
        userName: '用户B',
        content: '同意你的观点',
        createTime: '2024-04-05 10:30:00'
      },
      {
        id: 2,
        userName: '用户C',
        content: '我也觉得不错',
        createTime: '2024-04-05 11:00:00'
      }
    ]
  },
  {
    id: 2,
    bookName: '示例书籍2',
    userName: '用户D',
    content: '这是另一条评论内容，内容一般...',
    rating: 3,
    status: '隐藏',
    createTime: '2024-04-05 11:00:00',
    likes: 5,
    replies: 1,
    replyList: [
      {
        id: 3,
        userName: '用户E',
        content: '确实如此',
        createTime: '2024-04-05 11:30:00'
      }
    ]
  }
])

// 对话框相关
const dialogVisible = ref(false)
const currentComment = ref({})

// 搜索
const handleSearch = () => {
  // TODO: 调用搜索接口
  console.log('搜索关键词：', searchQuery.value)
}

// 查看详情
const handleDetail = (row) => {
  currentComment.value = row
  dialogVisible.value = true
}

// 切换评论状态
const handleToggleStatus = (row) => {
  const newStatus = row.status === '正常' ? '隐藏' : '正常'
  ElMessageBox.confirm(
    `确定要${newStatus}该评论吗？`,
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 调用状态更新接口
    ElMessage.success(`${newStatus}成功`)
  }).catch(() => {})
}

// 删除评论
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确定要删除该评论吗？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 调用删除接口
    ElMessage.success('删除成功')
  }).catch(() => {})
}

// 分页大小改变
const handleSizeChange = (val) => {
  pageSize.value = val
  // TODO: 重新加载数据
}

// 当前页改变
const handleCurrentChange = (val) => {
  currentPage.value = val
  // TODO: 重新加载数据
}
</script>

<style scoped lang="scss">
.comment-container {
  padding: 20px;
}

.comment-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .header-operations {
    display: flex;
    align-items: center;
  }
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.reply-list {
  margin-top: 20px;
  border-top: 1px solid #eee;
  padding-top: 20px;

  .reply-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 15px;
  }

  .reply-item {
    padding: 10px;
    border-bottom: 1px solid #eee;

    .reply-user {
      font-weight: bold;
      color: #409EFF;
    }

    .reply-content {
      margin: 5px 0;
    }

    .reply-time {
      font-size: 12px;
      color: #999;
    }
  }
}
</style> 